using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Microsoft.Xna.Framework;

using HadronData;

namespace HadronEditor
{
    public partial class LayerPropertiesForm : Form
    {
        private Level m_level;
        private Layer m_layer;

        private void m_btnOK_Click(object p_objSender, EventArgs p_eventArgs)
        {
            bool bError = false;

            // layer name
            if (m_txtName.Text.Length == 0)
            {
                bError = true;
                m_errorProvider.SetError(m_txtName, "No layer name specified");
            }
            else
            {
                Layer layer = m_level.GetLayer(m_txtName.Text);
                if (layer != null && layer != m_layer)
                {
                    bError = true;
                    m_errorProvider.SetError(m_txtName, "Duplicate layer name");
                }
            }

            // position
            Vector2 vecPosition = m_layer.Position;
            if (!float.TryParse(m_txtPositionX.Text, out vecPosition.X))
            {
                bError = true;
                m_errorProvider.SetError(m_txtPositionX, "Invalid position X coordinate");
            }
            if (!float.TryParse(m_txtPositionY.Text, out vecPosition.Y))
            {
                bError = true;
                m_errorProvider.SetError(m_txtPositionY, "Invalid position Y coordinate");
            }

            // orientation
            float fOrientation = MathHelper.ToDegrees(m_layer.Orientation);
            if (!float.TryParse(m_txtOrientation.Text, out fOrientation))
            {
                bError = true;
                m_errorProvider.SetError(m_txtOrientation, "Invalid orientation");
            }

            // angular speed
            float fAngularSpeed = MathHelper.ToDegrees(m_layer.AngularSpeed);
            if (!float.TryParse(m_txtAngularSpeed.Text, out fAngularSpeed))
            {
                bError = true;
                m_errorProvider.SetError(m_txtAngularSpeed, "Invalid orientation");
            }

            if (!bError)
            {
                m_layer.Name = m_txtName.Text;
                m_layer.Position = vecPosition;
                m_layer.Orientation = MathHelper.ToRadians(fOrientation);
                m_layer.AngularSpeed = MathHelper.ToRadians(fAngularSpeed);

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        public LayerPropertiesForm(Level p_level, Layer p_layer)
        {
            InitializeComponent();

            m_level = p_level;
            m_layer = p_layer;
            m_txtName.Text = m_layer.Name;
            m_txtPositionX.Text = ((int)m_layer.Position.X).ToString();
            m_txtPositionY.Text = ((int)m_layer.Position.Y).ToString();
            m_txtOrientation.Text = ((int) MathHelper.ToDegrees(m_layer.Orientation)).ToString();
            m_txtAngularSpeed.Text = ((int)MathHelper.ToDegrees(m_layer.AngularSpeed)).ToString();
        }
    }
}